import { createRouter, createWebHashHistory } from 'vue-router'
import { startLoading, closeLoading } from '@/utils/nprogress'
import LoginView from '../views/Login.vue'
import MainView from '../views/Main.vue'
// 可以使用动态的方法配置路由
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: LoginView
    },
    {
      path: '/myDemo',
      name: 'myDemo',
      component: () => import('../views/MyDemo.vue')
    },
    {
      path: '/layout',
      name: 'layout',
      component: () => import('../views/Layout.vue')
    },
    {
      path: '/main',
      name: 'main',
      component: MainView,
      redirect: '/home',
      children: [
        {
          path: '/home',
          name: 'home',
          component: () => import('../views/Home.vue')
        },
        {
          path: '/tailwind',
          name: 'tailwind',
          component: () => import('../views/Tailwind.vue')
        },
        {
          path: '/swiper',
          name: 'swiper',
          component: () => import('../views/Swiper.vue')
        },
        {
          path: '/gsap',
          name: 'gsap',
          component: () => import('../views/Gasp.vue')
        },
        {
          path: '/javascript',
          name: 'javascript',
          component: () => import('../views/Javascript.vue')
        },
        {
          path: '/:catchAll(.*)',
          name: 'error',
          component: () => import('../views/Error.vue')
        }
      ]
    },
    {
      path: '/detail',
      name: 'detail',
      component: () => import('../views/Detail.vue')
    },
    {
      path: '/vue3',
      name: 'vue3',
      component: () => import('../views/vue3.vue')
    },
    {
      path: '/test',
      name: 'test',
      component: () => import('../views/Test.vue')
    },
    {
      path: '/:catchAll(.*)',
      name: 'error',
      component: () => import('../views/Error.vue')
    }
  ]
})

// 监听路由的变化
router.beforeEach((to, from, next) => {
  startLoading()
  // const token = window.localStorage.getItem('token')
  // if (!token && to.path != '/') {
  //   next('/')
  // } else {
  //   next()
  // }
  next()
})
router.afterEach(() => {
  closeLoading()
})
export default router
